package com.css.zfzx.data.util;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @Description: data
 * @Author: lhl
 * @CreateDate: 2021/9/23 17:36
 */
public class ServerUtil {
    public static void addErrorMsg(StringBuilder builder, String field, String msg) {
        builder.append(field);
        builder.append(": ");
        builder.append(msg);
        builder.append(";");
    }

    public static void addNullMsg(StringBuilder builder, String field) {
        ServerUtil.addErrorMsg(builder,field,"为空");
    }

    /**
     * 根据属性，获取get方法
     * @param ob 对象
     * @param name 属性名
     * @return
     * @throws Exception
     */
    public static Object getGetMethod(Object ob , String name)throws Exception{
        Method[] m = ob.getClass().getMethods();
        for(int i = 0;i < m.length;i++){
            if(("get" + name).toLowerCase().equals(m[i].getName().toLowerCase())){
                return m[i].invoke(ob);
            }
        }
        return null;
    }

    public static void checkFieldNull(Object ob, Map<String,String> methodNames, StringBuilder builder) throws Exception{
        if(PlatformObjectUtils.isEmpty(ob) || PlatformObjectUtils.isEmpty(methodNames) || PlatformObjectUtils.isEmpty(builder)){
            return;
        }

        for(Map.Entry<String,String> entry : methodNames.entrySet()){
            Object obj=  ServerUtil.getGetMethod(ob,entry.getKey());
            if(obj==null || obj.equals("") || obj.equals("null")){
                addNullMsg(builder,entry.getValue());
            }
            /*if(PlatformObjectUtils.isEmpty(ServerUtil.getGetMethod(ob,entry.getKey()))){
                addNullMsg(builder,entry.getValue());
            }*/
        }
    }
}
